package com.pool.socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.HashMap;

public class DataSender extends Thread {

	private HashMap<Socket, ByteBuffer> toBeSend;

	DataSender() {
		toBeSend = new HashMap<Socket, ByteBuffer>();
	}

	public void run() {
		try {
			while (true) {
				synchronized (toBeSend) {
					while (toBeSend.isEmpty())
						toBeSend.wait();
					for (Socket conn : toBeSend.keySet()) {
						OutputStream out = conn.getOutputStream();
						ByteBuffer bb = toBeSend.get(conn);
						out.write(bb.array());
					}
					toBeSend.clear();
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public HashMap<Socket, ByteBuffer> getToBeSend() {
		return toBeSend;
	}

	public void setToBeSend(HashMap<Socket, ByteBuffer> toBeSend) {
		this.toBeSend = toBeSend;
	}

}
